home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Palettes / PAStringList / PAStringList.m < prev    next >
Text File  |  1995-06-12  |  14KB  |  369 lines

  1. #import "PAStringList.h"
  2.  
  3. /******************************************************************************
  4. PAStringList
  5.  
  6.     The PAStringList (a subclass of Storage) is a convenient way to deal with lists of character strings. It contains methods for adding strings, inserting strings, sorting strings and searching for strings.
  7.         
  8.     The PAStringList can also add strings from a delimited string (such as the ones passed to an app by the workspace). The delimiters can be supplied or assumed to be white space.
  9.     
  10.     Finally, the PAStringList implements a browser delegate method so that it can easily display itself if set to be a browser delegate.
  11.  
  12.     To use this object inside of InterfaceBuilder, simply drag its icon from the palettes window into the suitcases window. Then drag in a browser and set its delegate to be the stringlist. Type in some entries into PAStringList's inspector and test interface. The browser fills up!
  13.  
  14.     Copyright 1992, Jeff Martin (jmartin@next.com) (415) 780-3833.
  15. ******************************************************************************/
  16.     
  17. @implementation PAStringList : Storage
  18.  
  19. - init
  20.     // Customize Storage to be 'char *' only
  21.     [super initCount:0 elementSize:4 description:"*"];
  22.  
  23.     // StringList is assumed to be sorted by default and when emptied
  24.     //    Adding a string without sorted flag causes 'isSorted' to be set to NO
  25.     isSorted = YES;
  26.     return self; 
  27. }
  28.  
  29. /******************************************************************************
  30.     addString:, addStringIfAbsent: addStringNoCopy: addStringIfAbsentNoCopy:
  31.     addString:at:, addStringIfAbsent:at:, addStringIfAbsent:at:
  32.     addStringSorted:, addStringIfAbsentSorted:, addStringIfAbsentNoCopySorted:
  33.  
  34.     These methods are convenience methods that simply call the master addString method and provide shorter names for cleaner use assuming the defaults of ifAbsent=NO, noCopy=NO, sorted=NO and at=count. Returns the result of the main addString method (self).
  35. ******************************************************************************/
  36. - addString:(const char *)string { return 
  37.     [self addString:string ifAbsent:NO noCopy:NO sorted:NO at:[self count]];  }
  38. - addStringIfAbsent:(const char *)string { return 
  39.     [self addString:string ifAbsent:YES noCopy:NO sorted:NO at:[self count]]; }
  40. - addStringNoCopy:(const char *)string { return 
  41.     [self addString:string ifAbsent:NO noCopy:YES sorted:NO at:[self count]]; }
  42. - addStringIfAbsentNoCopy:(const char *)string { return
  43.     [self addString:string ifAbsent:YES noCopy:YES sorted:NO at:[self count]];}
  44.  
  45. - addString:(const char *)string at:(int)at;
  46. { return [self addString:string ifAbsent:NO noCopy:NO sorted:NO at:at]; }
  47. - addStringIfAbsent:(const char *)string at:(int)at;
  48. { return [self addString:string ifAbsent:YES noCopy:NO sorted:NO at:at]; }
  49. - addStringNoCopy:(const char *)string at:(int)at;
  50. { return [self addString:string ifAbsent:NO noCopy:YES sorted:NO at:at]; }
  51. - addStringIfAbsentNoCopy:(const char *)string at:(int)at;
  52. { return [self addString:string ifAbsent:YES noCopy:YES sorted:NO at:at]; }
  53.  
  54. - addStringSorted:(const char *)string;
  55. { return [self addString:string ifAbsent:NO noCopy:NO sorted:YES at:0]; }
  56. - addStringIfAbsentSorted:(const char *)string;
  57. { return [self addString:string ifAbsent:YES noCopy:NO sorted:YES at:0]; }
  58. - addStringNoCopySorted:(const char *)string;
  59. { return [self addString:string ifAbsent:NO noCopy:YES sorted:YES at:0]; }
  60. - addStringIfAbsentNoCopySorted:(const char *)string;
  61. { return [self addString:string ifAbsent:YES noCopy:YES sorted:YES at:0]; }
  62.  
  63. /******************************************************************************
  64.     addString:ifAbsent:noCopy:sorted:at:
  65.  
  66.     This method provides a flexible method for adding strings to the list. The ifAbsent flag specifies whether a string should be added if it already exists. The noCopy flag specifies whether the given string should be used (or copied). The sorted flag specifies whether the string should be added alphabetically. The at value specifies where the string should be inserted at if it is not added alphabetically. Returns self.
  67. ******************************************************************************/
  68. - addString:(const char *)string ifAbsent:(BOOL)ifAbsent noCopy:(BOOL)noCopy sorted:(BOOL)sorted at:(int)at
  69. {
  70.     BOOL stringExists;
  71.     int index = (ifAbsent || sorted)? 
  72.         [self indexOfString:string exists:&stringExists] : 0;
  73.  
  74.     // If we add only if absent and string is in list return self
  75.     if(ifAbsent && stringExists) return self;
  76.  
  77.     // If not noCopy (in other words, if copy) make copy
  78.     if(!noCopy) string = NXCopyStringBufferFromZone(string, [self zone]);
  79.  
  80.     // If sorted, get index else add at 'at'; otherwise set isSorted flag to NO
  81.     if(sorted) at = index; else isSorted = NO;
  82.     
  83.     // Add the string and return
  84.     [self insertElement:(char **)&string at:at];
  85.     return self;
  86. }
  87.  
  88. /******************************************************************************
  89.     addStrings, addPAStringList
  90.  
  91.     These methods allow for lists of strings to be added either from a char ** or from a PAStringList object. Returns self.
  92. ******************************************************************************/
  93. - addStrings:(const char *const*)strings { return 
  94.     [self addStrings:strings ifAbsent:NO noCopy:NO sorted:NO at:[self count]];}
  95. - addStrings:(const char *const*)strings ifAbsent:(BOOL)ifAbsent noCopy:(BOOL)noCopy sorted:(BOOL)sorted at:(int)at
  96. {
  97.     char **temp = (char **)strings;
  98.     
  99.     // Add each string individually, incrementing 'at' to preserve their order
  100.     while(*temp) {
  101.         [self addString:*temp ifAbsent:ifAbsent noCopy:noCopy 
  102.             sorted:sorted at:at];
  103.         at++; temp++;
  104.     }
  105.     
  106.     // If 'noCopy' then we own the memory 'strings' and should free it
  107.     if(noCopy) free((char *)strings);
  108.     
  109.     return self;
  110. }
  111.  
  112. - addPAStringList:stringListObject 
  113. { return [self addStrings:[stringListObject strings]]; }
  114. - addPAStringList:stringListObject ifAbsent:(BOOL)ifAbsent noCopy:(BOOL)noCopy sorted:(BOOL)sorted at:(int)at
  115. { return [self addStrings:[stringListObject strings] ifAbsent:ifAbsent 
  116.         noCopy:noCopy sorted:sorted at:at]; }
  117.  
  118. /******************************************************************************
  119.     addDelimitedStrings:delimiters:
  120.     addDelimitedStrings:delimiters:ifAbsent:sorted:at:
  121.  
  122.     This method takes a delimited string (like the ones passed from the workspace) and searches for the given delimiters (NULL for general whitespace). It adds each string that it finds between the delimiters using the addString method. Returns self.
  123. ******************************************************************************/
  124. - addDelimitedStrings:(const char *)string delimiters:(const char *)dels
  125. { return [self addDelimitedStrings:string delimiters:dels ifAbsent:NO  sorted:NO at:[self count]]; }
  126. - addDelimitedStrings:(const char *)string delimiters:(const char *)dels ifAbsent:(BOOL)ifAbsent sorted:(BOOL)sorted at:(int)at
  127. {
  128.     static char defaultDels[] = {' ', '\t', '\n', '\r', '\0'};
  129.     int delCount, i;
  130.     char *currChar, *currString, *stringEnd;
  131.  
  132.     // Check to see if we were handed a bogus string
  133.     if(!string || !strlen(string)) return self;
  134.  
  135.     // Check for default delimiter and get number of delimiters
  136.     if(!dels) dels = defaultDels;
  137.     delCount = strlen(dels) + 1;
  138.     
  139.     // Make copy of the string (going to use string as scratch)
  140.     string = NXCopyStringBuffer(string);
  141.     currChar = currString = (char *)string; 
  142.     stringEnd = (char *)((int)string+strlen(string));
  143.     
  144.     // Look at each character until we pass null terminator(stringEnd)
  145.     while(currChar <= stringEnd) {
  146.         
  147.         // Check the character against the delimiters
  148.         for(i=0; i<delCount; i++)
  149.  
  150.             // If current character matches a delimiter add current string
  151.             if(*currChar==dels[i]) {
  152.             
  153.                 // Set delimiter to NULL('\0')
  154.                 *currChar = '\0'; 
  155.                 
  156.                 // Only add if there is something to add. Increment at.
  157.                 if(strlen(currString))
  158.                     [self addString:currString ifAbsent:ifAbsent noCopy:NO 
  159.                         sorted:sorted at:at++];
  160.  
  161.                 // set currString to start of next string(just past curr del)
  162.                 currString = currChar + 1;
  163.                 break;
  164.             }
  165.         currChar++;
  166.     }
  167.     
  168.     free((char *)string);
  169.     return self;
  170. }
  171.  
  172.  
  173. /******************************************************************************
  174.     (const char *const*)strings
  175.     (const char *)stringAt:(int)at
  176.  
  177.     The strings methods returns all of the strings as an array of char pointers. This array is NOT null terminated.
  178.     The stringAt: method returns the string at a particular index.
  179. ******************************************************************************/
  180. - (const char *const*)strings     { return (const char *const*)dataPtr; }
  181. - (const char *)stringAt:(int)at { return *(char **)[self elementAt:at]; }
  182.  
  183.  
  184. /******************************************************************************
  185.     (BOOL)stringExists
  186.     (unsigned int)indexOfString
  187.     (unsigned int)indexOfString exists:(BOOL *)exists
  188.  
  189.     stringExists returns whether or not the string is already in the list. indexOfString returns either the strings current index or the index that it should be alphabetically(returning [self count] if not sorted).
  190.     indexOfString:stringExists: is a composite method that returns both values in about the same amount of time that it takes to compute one.
  191. ******************************************************************************/
  192. - (BOOL)stringExists:(const char *)string
  193. { BOOL exists; [self indexOfString:string exists:&exists]; return exists; }
  194.  
  195. - (unsigned)indexOfString:(const char *)string
  196. { return [self indexOfString:string exists:NULL]; }
  197.  
  198. - (unsigned)indexOfString:(const char *)string exists:(BOOL *)exists
  199. {
  200.     int index;
  201.     
  202.     // Assume the string won't be found
  203.     if(exists) *exists = NO;
  204.  
  205.     // If list is empty or no string or zero strlen, return end of list
  206.     if(![self count] || !string || !strlen(string)) return [self count];
  207.     
  208.     // If not sorted do sequential search
  209.     if(!isSorted) {
  210.         int i=0;
  211.         for(i=0; i<[self count]; i++) 
  212.             if([self stringAt:i] && (!strcasecmp(string, [self stringAt:i]))) 
  213.                 { if(exists) *exists = YES; break; }
  214.  
  215.         index = i;
  216.     }
  217.     
  218.     // Otherwise if it is sorted do a binary search
  219.     else {
  220.         int l = 0;                        // lower index
  221.         int u = [self count] - 1;        // upper index
  222.         int m = 0;                        // middle index
  223.         int guess = 0;                    // compare val.
  224.         
  225.         while(l <= u) {
  226.             m = (l+u)/2;
  227.             guess = strcasecmp([self stringAt:m], string);
  228.             
  229.             // If guess is too high, adjust the upper value
  230.             if(guess>0) u = m-1;
  231.             
  232.             // If guess is too low, adjust the lower value
  233.             else if(guess<0) l = m+1;
  234.             
  235.             // If guess is equal to string, set 'exists' flag and break
  236.             else { if(exists) *exists = YES; break; }
  237.         }
  238.  
  239.         // If last guess was right or too high index is m; if too low then m+1;
  240.         if(guess>=0) index = m; else index = m+1;
  241.     }
  242.  
  243.     return index;
  244. }
  245.  
  246.  
  247. /******************************************************************************
  248.     removeString:(const char *)string
  249.     removeStrings:(const char *const*)strings
  250.     removeStringAt:(int)at
  251.  
  252.     These methods allow for the removal of strings by value or index. Returns self.
  253. ******************************************************************************/
  254. - removeString:(const char *)string
  255. {
  256.     BOOL exists;
  257.     int index = [self indexOfString:string exists:&exists];
  258.     if(exists) [self removeStringAt:index];
  259.     return self;
  260. }
  261. - removeStrings:(const char *const*)strings
  262. {
  263.     int i;
  264.     for(i=0; i<[self count]; i++) [self removeString:strings[i]];
  265.     return self;
  266. }
  267. - (char *)removeStringAt:(int)at
  268. {
  269.     char *string = (char *)[self stringAt:at];
  270.     [self removeElementAt:at];
  271.     if(![self count]) isSorted = YES;
  272.     return string;
  273. }
  274.  
  275. /******************************************************************************
  276.     (BOOL)isSorted
  277.     sortStrings:sender
  278.     
  279.     isSorted returns whether or not the list is currently considered to be sorted. Lists are set to be sorted by default and when empty. isSorted is set to NO when a string is added without the 'sorted' flag.
  280.     sortStrings: sorts the stringList (ignoring case) and sets the sorted flag. Returns self.
  281. ******************************************************************************/
  282. - (BOOL)isSorted    { return isSorted; }
  283.  
  284. // Wrap around strcasecmp to accept 'char **' and NULL strings(NULLs to back)
  285. static int strPtrCaseCmp(const void *s1, const void *s2)
  286. {
  287.     if(*(char **)s1==*(char **)s2) return 0;
  288.     else if(!*(char **)s1) return 1; else if(!*(char **)s2) return -1;
  289.     else return strcasecmp(*((char **)s1),*((char **)s2));
  290. }
  291.  
  292. - sortStrings:sender
  293. {
  294.     qsort(dataPtr, [self count], sizeof(char *), strPtrCaseCmp); 
  295.     isSorted = YES;
  296.     return self;
  297. }
  298.  
  299. /******************************************************************************
  300.     Write and read the PAStringList for archiving.
  301. ******************************************************************************/
  302. - write:(NXTypedStream *)stream
  303. {    
  304.     [super write:stream];
  305.     NXWriteType(stream, "c", &isSorted);
  306.     return self;
  307. }
  308.  
  309. - read:(NXTypedStream *)stream
  310. {
  311.     [super read:stream];
  312.     NXReadType(stream, "c", &isSorted);
  313.     return self;
  314. }
  315.  
  316. /******************************************************************************
  317.     empty, freeStrings, free
  318.     
  319.     These methods empty the list, free the strings and free the list, respectively.
  320. ******************************************************************************/
  321. - empty { isSorted = YES; return [super empty]; }
  322.  
  323. - freeStrings
  324. {
  325.     while([self count]) free([self removeStringAt:0]); 
  326.     return self;
  327. }
  328.  
  329. - free
  330.     return [super free];
  331. }
  332.  
  333. /******************************************************************************
  334.     (int)browser:sender fillMatrix:matrix inColumn:(int)column
  335.     
  336.     This method is provided as a delegate method for browser to quickly display string list.
  337. ******************************************************************************/
  338. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  339. {
  340.     int   i;
  341.     id   cellList, theCell;
  342.   
  343.     // Set matrix to have the right number of cells.
  344.     [matrix renewRows:[self count] cols:1];
  345.  
  346.     // Get list of cells from the matrix.
  347.     cellList = [matrix cellList];
  348.  
  349.     // For each cell set its value, set whether it is a leaf or not and 
  350.     //   mark it loaded.
  351.     for(i=0;i<[cellList count];i++) {
  352.         theCell = [cellList objectAt:i];
  353.         [theCell setStringValue:[self stringAt:i]];
  354.         [theCell setLeaf:YES];
  355.         [theCell setLoaded:YES];
  356.     }
  357.  
  358.     // Return the number of rows.
  359.     return [self count];
  360. }
  361.  
  362. // Interface Builder support
  363. - (const char *)getInspectorClassName { return "PAStringListInspector"; }
  364.  
  365. @end
  366.  
  367. // Fixed indexOf: bug. Thanks Stefanie Herzer!